/*
 *
 * Copyright (c) 2023 - future
 * @author fzh
 * @email e_341877120_mail@163.com
 * @link https://mvnrepository.com/search?q=io.github.xrfzh.cn
 *
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package cn.xrfzh.log.receiver;

import com.alibaba.fastjson2.JSONObject;
import cn.xrfzh.common.entity.ExceptionLog;
import cn.xrfzh.common.entity.OperationLog;
import cn.xrfzh.log.service.IExceptionLogService;
import cn.xrfzh.log.service.IOperationLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Slf4j
@Component
public class LogReceiver {

	@Resource
	private IOperationLogService operationLogService;

	@Resource
	private IExceptionLogService exceptionLogService;

	/**
	 * 接收操作日志数据，持久化到数据库
	 */
	@RabbitListener(queues = "operationLogQueue")
	public void receiveOperationLog(@Payload String operationLogJsonStr) {
		log.info("当前时间戳<{}>，LogReceiver接收消息<{}>", System.currentTimeMillis(), operationLogJsonStr);
		operationLogService.save(JSONObject.parseObject(operationLogJsonStr, OperationLog.class));
	}

	/**
	 * 接收异常日志数据，持久化到数据库
	 */
	@RabbitListener(queues = "exceptionLogQueue")
	public void receiveExceptionLog(@Payload String exceptionLogJsonStr) {
		log.info("当前时间戳<{}>，LogReceiver接收消息<{}>", System.currentTimeMillis(), exceptionLogJsonStr);
		exceptionLogService.save(JSONObject.parseObject(exceptionLogJsonStr, ExceptionLog.class));
	}

}
